<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/11
 * Time: 15:52
 */

namespace zhtfrontend\controllers;

use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use common\models\datas\Customer;
use zhtfrontend\components\bases\BaseController;
use zhtfrontend\models\datas\Mechanic;
use zhtfrontend\models\datas\Orders;
use zhtfrontend\models\datas\OrdersItems;
use yii\data\Pagination;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\Response;

class ConsumptionController extends BaseController
{
    /**
     * 会员消费统计
     */
    public function actionIndex()
    {
        $param['searchData'] = \Yii::$app->request->get();
        $pageSize = !empty($param['pageSize']) ? $param['pageSize'] : 12;
        $data = Orders::find()->where(['>', 'pay_status', 0])->orderBy('id DESC');
        if ($param['searchData']) {
            $search_data = $param['searchData'];
            if (!empty($search_data['mechanic_id'])) {
                $orders_ids = OrdersItems::find()->select(['orders_id'])->where(['mechanic_id' => $search_data['mechanic_id']])->groupBy('orders_id')->column();
                $data->andWhere(['id' => $orders_ids]);
            }
            if (!empty($search_data['searchWay'])) {
                if ($search_data['searchWay'] == 2) { //按日搜索
                    $data->andWhere(['between', 'created_at', strtotime(date('Y-m-d', strtotime($search_data['searchDate']))), strtotime(date('Y-m-d', strtotime("+1 day", strtotime($search_data['searchDate']))))]);
                } else { //按月搜索
                    $data->andWhere(['between', 'created_at', strtotime(date('Y-m', strtotime($search_data['searchDate']))), strtotime(date('Y-m', strtotime("next month", strtotime($search_data['searchDate']))))]);
                }
            }
            if (!empty($search_data['ordersType'])) {
                if ($search_data['ordersType'] == 1) {//预约
                    $data->andWhere(['order_type' => 'appointment']);
                } elseif ($search_data['ordersType'] == 2) {//退定金
                    $data->andWhere(['in', 'pay_status', [4, 5]]);
                } elseif ($search_data['ordersType'] == 3) {//退课
                    $data->andWhere(['in', 'status', [4, 5]]);
                } else {//消费
                    $data->andWhere(['!=', 'order_type', 'appointment']);
                }
            }
            if (!empty($search_data['tel'])) {
                $data->andWhere(['telephone' => $search_data['tel']]);
            }
            if (!empty($search_data['name'])) {
                $customer = Customer::find()->where(['like', 'name', $search_data['name']])->one();
                if ($customer != null)
                    $data->andWhere(['customer_id' => $customer->id]);
            }
        }

        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($model, ['customer_name']);
        $content = "<div class='yx-total-box'><h4 class='yx-total1 yx-floatl'>总计:<span class='yx-color2cb4f3 yx-font16 yx-mr5'> {$data->count()} </span>条</h4><h4 class='yx-total1 yx-floatl'>消费金额：<span class='yx-color2cb4f3 yx-font16 yx-mr5'>{$data->sum('remain_amount')}</span> 元</h4><h4 class='yx-total1 yx-floatl'>优惠金额：<span class='yx-color2cb4f3 yx-font16 yx-mr5'>{$data->sum('discount_amount')}</span> 元</h4><div class='yx-clear'></div>";

        $modelMechanic = Mechanic::find();
        $mechanicId = Fun::getMechanicIds();
        if (!empty($mechanicId)) {
            $modelMechanic->andwhere(['in', 'id', $mechanicId]);
        }
        $DirectorMechanicIds = Fun::getDirectorMechanicIds();
        if (isset(\Yii::$app->user->identity) && \Yii::$app->user->identity->role == 'business_director') {
            $modelMechanic->andWhere(['id' => $DirectorMechanicIds]);
        }
        $modelMechanic = $modelMechanic->all();
        $listData = ArrayHelper::map($modelMechanic, 'id', 'name');

        return $this->render('index', ['listData' => $listData, 'data' => $array, 'pages' => $pages, 'content' => $content]);
    }
}